<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @link http://book.cakephp.org/view/957/The-App-Controller
 * 
 */

// Allow sanitizing
App::import('Sanitize');

class AppController extends Controller {
    
    var $helpers = array('Html', 'Form', 'Layout', 'Session', 'Popup'/*'Javascript', 'Ajax' */);
    var $components = array('Session', 'Auth');
    
    
    /**
     * Called before filtering the data
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function beforeFilter() {
        parent::beforeFilter();
        
	// Authenticate
	$this->Auth->loginAction = array(
             'controller' => 'app', 
             'action' => 'personalPage'
        );        
	$this->Auth->loginError = 'Fehler bei der Anmeldung. Unbekannter Nutzername oder Passwort!';        
	$this->Auth->logoutRedirect = array(
             'controller' => 'tutorials', 
             'action' => 'index'
        );        
        $this->Auth->fields = array(
             'username' => 'username',
             'password' => 'password'
        );
        
        // Allow all
        $this->Auth->allow('*');
        
        // Check, if user is logged into system
        if (is_array($this->Session->read('Auth.User'))) {
            $this->set('user', $this->Session->read('Auth.User'));     
            $this->Auth->deny('register', 'login');
        } else {
            $this->Auth->deny('edit', 'delete', 'add');
        }   
        
        // Default caption is empty
        $this->set('caption', '');
        
        // Load categories
        App::import('Model', 'Category');
        $category_model = new Category();
        
        // Load tutorials
        App::import('Model', 'Tutorial');
        $tutorial_model = new Tutorial();     
        
        // Sort tutorials
        $params = array(
             'order' => 'Tutorial.created ASC',
             'limit' => 5
        );
        
        // Set data
        $this->set('new_tutorials', $tutorial_model->find('all', $params));
        $this->set('categories', $category_model->find('all'));   
    }
    
    
    
    
    
    /**
     * Called before rendering the screen
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function beforeRender() {
        parent::beforeRender();
    }   
    
    
    
    
    /**
     * Call a custom flash box with a message inside
     * 
     * ### Options:
     *
     * - success (string) Shows a green dialog of success
     * - fail (string) Shows a red dialog of fail
     * - warning (string) Shows an orange dialog of warning
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function flashMessage($message, $type) {
        $this->Session->setFlash($message, 'flash', array('class' => $type));
    }
    
    
    
    
    /**
     * Redirect to the current user's page directly.
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function personalPage() {
        $this->redirect(array('controller' => 'users', 'action' => 'view/' . $this->Session->read('Auth.User.id')));
    }
    
    
    
    
    
    /**
     * Check, if user has admin rights
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function isAdminUser($user) {
        return $this->Session->read('Auth.User.rank')  == 'admin';
    }
    
    
    
    
    
    /**
     * Check, if user is the local user
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function isCurrentUser($user) {
        return $this->Session->read('Auth.User.id')  == $user['User']['id'];
    }
    
    
    
    
    
    /**
     * Set the title for browser tab
     * 
     * @author      Miguel Gonzalez
     * @since       TutoriumBase v 0.0
     */
    function setPageTitle($text) {
        Configure::load('configuration');
        $appname = Configure::read('About.name');
        $this->set('title_for_layout', $appname . ' - ' . $text);
    }   
}